package app

import (
	"chatgpt/helper/code"
	"chatgpt/model/app"
	"chatgpt/services/datasrv/sqlhelper"
	"github.com/slclub/glog"
	"github.com/slclub/go-tips"
	"strings"
	"time"
)

type RegisterUser struct {
}

func (ru *RegisterUser) EventAction(user *app.User) {
	ru.freeVip(user)
}

func (ru *RegisterUser) freeVip(user *app.User) {
	benefitOnly := BenefitOnly{}
	beneSrv := BenefitSrv{}
	bens := benefitOnly.GetList(&sqlhelper.Where{
		Conds: []*sqlhelper.CondItem{
			sqlhelper.WhereCond("Action", app.BENEFIT_FREE_EXPIRE),
		},
	})
	if len(bens) == 0 {
		return
	}
	for _, bene := range bens {
		expire := 1
		if tips.StrPos(bene.Data, "*") >= 0 {
			ve := strings.Split(bene.Data, "*")
			for _, v := range ve {
				expire *= tips.Int(v)
			}

		} else {
			expire = tips.Int(bene.Data)
		}
		mc := beneSrv.CallAttachWithID(user.UUID, bene.BenefitID, time.Duration(tips.Any2Int64(expire)))
		if mc != code.SUCCESS {
			glog.Errorf("Register.EventAction freeVip UUID:=%v BenefitID:=%v", user.UUID, bene.BenefitID)
		}
	}
	glog.Infof("Register.EventAction freeVip UUID:=%v ", user.UUID)
}
